package algorithm;

/**
 * Created with IntelliJ IDEA.
 * ClassName: HuiJin
 * Package: algorithm
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-19
 * Time: 20:35
 */

public class HuiJin {

    //长度11的数组 元素依次是1-10 第11个元素是1-10中其中一个 问怎么找到这个重复元素
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 8};
        System.out.println("遍历求解:" + ergodic(arr));

        System.out.println("累加求解:" + addUp(arr));
    }

    //遍历
    public static int ergodic(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int tmp = arr[i];
            for (int j = 0; j < arr.length && i != j; j++) {
                if (tmp == arr[j]) {
                    return arr[j];
                }
            }
        }
        return 0;
    }

    public static int addUp(int[] arr) {
        int sum = 0;
        int ten = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
            if (i == 9) {
                ten = sum;
            }
        }
        return sum - ten;
    }

}
